package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;
import com.yuwj.chinaa.util.ValidatonCheckUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity {
    IPresenter presenter;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_yanzm)
    EditText etYanzm;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.et_confirm_password)
    EditText etConfirmPassword;
    @BindView(R.id.tv_second)
    TextView tvSecond;

    private CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
        //参数1，设置倒计时的总时间（毫秒）
        //参数2，设置每次减去多少毫秒
        @Override
        public void onTick(long millisUntilFinished) {
            tvSecond.setEnabled(false);
            tvSecond.setText((millisUntilFinished / 1000) + "s");
        }

        @Override
        public void onFinish() {
            tvSecond.setEnabled(true);
            tvSecond.setText("发送验证码");
        }
    };

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_forget_password;
    }

    public static Intent createIntent(Context context) {
        return new Intent(context, ForgetPasswordActivity.class);
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initOperator() {

    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }

    @OnClick({R.id.ib_back, R.id.tv_second, R.id.tv_find_password})
    public void onViewClicked(View view) {
        String phone = etPhone.getText().toString();
        switch (view.getId()) {
            case R.id.ib_back:
                onBackPressed();
                break;
            case R.id.tv_second:

                if (TextUtils.isEmpty(phone) || !ValidatonCheckUtil.isMobile(phone)) {
                    ToastUtil.getInstance().shortLength(context, "请输入有效的手机号");
                    return;
                }
                JSONObject paramWithSendVerifyCode = new JSONObject();
                try {
                    paramWithSendVerifyCode.put("phone", phone);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.msgGetVerifyCode(paramWithSendVerifyCode.toString());
                timer.start();

                break;
            case R.id.tv_find_password:
                String msgCode = etYanzm.getText().toString();
                String newPassword = etNewPassword.getText().toString();
                String confirmPassword = etConfirmPassword.getText().toString();

                if (TextUtils.isEmpty(phone) || !ValidatonCheckUtil.isMobile(phone)) {
                    ToastUtil.getInstance().shortLength(context, "请输入有效的手机号");
                    return;
                }

                if (TextUtils.isEmpty(msgCode)) {
                    ToastUtil.getInstance().shortLength(context, "请输入验证码");
                    return;
                }

                if (TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(confirmPassword)) {
                    ToastUtil.getInstance().shortLength(context, "请输入新密码");
                    return;
                }


                if (!newPassword.equals(confirmPassword)) {
                    ToastUtil.getInstance().shortLength(context, "两次输入的密码不一致，请重新输入");
                    return;
                }

                if (!ValidatonCheckUtil.isLegalPassword(newPassword)) {
                    ToastUtil.getInstance().shortLength(context, "请输入6位及以上字符密码");
                    return;
                }

                JSONObject paramWithForgetPassword = new JSONObject();
                try {
                    paramWithForgetPassword.put("phone", phone);
                    paramWithForgetPassword.put("newpwd", newPassword);
                    paramWithForgetPassword.put("verifycode", msgCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.userForgetPassword(paramWithForgetPassword.toString());


                break;
        }
    }


    @Override
    public void onMsgGetVerifyCodeSuccess(String code) {
        super.onMsgGetVerifyCodeSuccess(code);
        ToastUtil.getInstance().shortLength(context, "验证码发送成功");
    }

    @Override
    public void onMsgGetVerifyCodeFail(Exception e) {
        super.onMsgGetVerifyCodeFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }

    @Override
    public void onUserForgetPasswordSuccess(String pwd) {
        super.onUserForgetPasswordSuccess(pwd);
        ToastUtil.getInstance().shortLength(context, "密码修改成功");
        finish();
    }

    @Override
    public void onUserForgetPasswordFail(Exception e) {
        super.onUserForgetPasswordFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }

}
